<?php
/**
 * EVDEV Input / Output
 *
 * @package Deenzo
 */

// No direct access
defined( "__DEENZO__" ) or die( "deenzo$" );

// Loading dependencies
coreutil( "env" )  or kill( "env$" );

/* LOOP register */
function loop( $ns, $v ) {
    
    if ( !is_array( $v ) ) return false;
    
    env("ns.$ns.loop" , $v);
    
    env("ns.$ns.count", count( $v ) );
    env("ns.$ns.i", 0 );
}

function the_loop( $ns ) {
    
    // Cursor Position ( next or rewind )
    $cursor = ( env("ns.$ns.i") < env("ns.$ns.count") );
    
    if ( $cursor ) 
        env("ns.$ns.i", env("ns.$ns.i") + 1 );
    else             
        env("ns.$ns.i", 0 );
     
    return $cursor; 
    
}

function get_the_field( $ns, $attr ) {

    $k = array_keys( env("ns.$ns.loop") );
    $k = $k[  env("ns.$ns.i") - 1 ];
           
    return env("ns.$ns.loop.$k.$attr" );
    
}

/* Echoes The FIELD */
function the_field( $ns, $attr ) {
    echo get_the_field( $ns, $attr );
}

/* Register a new LOOP */
function register_loop( $ns, $fields, $data ) {
    
    /* Check for same named LOOP */
	if ( !function_exists( $ns ) ) {
		eval("function $ns(" . '$v' . ") { loop('$ns', " . '$v' . "); }");
	
	    $ns( $data );
	}
    
	/* Registering the_loop-like function */
	if ( !function_exists( "the_$ns" ) )
		eval("function the_$ns() { return the_loop('$ns'); }");
    
	/* Registering the_fields-like functions */
    foreach ( $fields as $field ) {
        
        $func = $field;
        
        // Check if not using field mask
        if ( !is_string( $field ) )
            list( $func, $field ) = $field;
        
        /* Register the_field-like function */
        if ( !function_exists( "get_the_$func" ) )
            eval("function get_the_$func() { return get_the_field('$ns', '$field'); }");
        
        /* This function echoes field value */
        if ( !function_exists( "the_$func" ) )
            eval("function the_$func() { the_field('$ns', '$field'); }");
        
    }
}

/* X11 getter */
function getter( $k ) {
    return env( "X11.varz.$k" );
}

/** X11 setter */
function setter( $k, $v ) {
    env( "X11.varz.$k", $v );
}

/** X11 foot pool */
function foot( $func = "" ) {
    sched( "-pool 'X11.foot' -func '$func'" );
}

/** X11 head pool */
function head( $func = "" ) {
    sched( "-pool 'X11.head' -func '$func'" );
}

/** X11 meta pool */
function meta( $func = "" ) {
}